package net.homeftp.heimo.weddingwidget.perf;

import java.text.ParseException;
import java.util.Date;

import net.homeftp.heimo.weddingwidget.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

public class MyPreferenceManager extends BasicPreferenceManager {
	private static final String TAG = "MyPreferenceManager";
	private String prefBasicCountTypeKey, prefBasicAnniKey, prefBasicLayoutKey,
			prefBasicAlphaKey, prefCountBgKey, prefCountFontBgKey,
			prefCountPatternKey, prefCountThemeKey, prefTTextKey,
			prefTTextBgKey, prefTTextSizeKey, prefTTextFontBgKey, prefBTextKey,
			prefBTextBgKey, prefBTextSizeKey, prefBTextFontBgKey;

	public MyPreferenceManager(Context ctx, int appWidgetId) {
		
		// Get all Saved record from specific widgetId
		this(ctx);
		Log.d(TAG, "[Init()] MyPreferenceManager appWidgetId : "+appWidgetId);
		widgetConfigMode = true;
		appId = appWidgetId;
	}

	public MyPreferenceManager(Context ctx) {
		Log.d(TAG, "[Init()] MyPreferenceManager appWidgetId : "+this.appId);
		this.ctx = ctx;
		this.sPref = PreferenceManager.getDefaultSharedPreferences(ctx);

		this.prefBasicCountTypeKey = ctx.getResources().getString(
				R.string.pref_counter_type_key);
		this.prefBasicAnniKey = ctx.getString(R.string.pref_basic_anni_key);
		this.prefBasicLayoutKey = ctx.getString(R.string.pref_basic_layout_key);
		this.prefBasicAlphaKey = ctx.getResources().getString(
				R.string.pref_basic_alpha_key);

		this.prefCountBgKey = ctx.getResources().getString(
				R.string.pref_counter_bg_key);
		this.prefCountFontBgKey = ctx.getResources().getString(
				R.string.pref_counter_fontbg_key);
		this.prefCountPatternKey = ctx
				.getString(R.string.pref_counter_pattern_key);
		this.prefCountThemeKey = ctx.getResources().getString(
				R.string.pref_counter_theme_key);

		this.prefTTextKey = ctx.getString(R.string.pref_toptext_key);
		this.prefTTextBgKey = ctx.getString(R.string.pref_toptext_bg_key);
		this.prefTTextFontBgKey = ctx
				.getString(R.string.pref_toptext_fontbg_key);
		this.prefTTextSizeKey = ctx.getString(R.string.pref_toptext_size_key);

		this.prefBTextKey = ctx.getString(R.string.pref_bottomtext_key);
		this.prefBTextBgKey = ctx.getString(R.string.pref_bottomtext_bg_key);
		this.prefBTextFontBgKey = ctx
				.getString(R.string.pref_bottomtext_font_bg_key);
		this.prefBTextSizeKey = ctx
				.getString(R.string.pref_bottomtext_size_key);
	}

	/*
	 * Handler of Counter Type - Anniversary - Count Down
	 */
	public void setPrefCounterType(int prefCountType) {
		fnSavePref(prefBasicCountTypeKey, prefCountType);
	}

	public void setPrefCounterType(String prefCountType) {
		final String[] itemsName = ctx.getResources().getStringArray(
				R.array.string_array_counter_type_name);
		final int[] itemsValue = ctx.getResources().getIntArray(
				R.array.int_array_counter_type_value);
		for (int i = 0; i < itemsName.length; i++) {
			if (itemsName[i].equalsIgnoreCase(prefCountType)) {
				setPrefCounterType(itemsValue[i]);
			}
		}
	}

	public int getPrefCounterType() {
		return (Integer)fnGetPref(prefBasicCountTypeKey, 1);
	}

	public String getPrefCounterTypeName() {
		int itemValue = getPrefCounterType();
		final String[] itemsName = ctx.getResources().getStringArray(
				R.array.string_array_counter_type_name);
		final int[] itemsValue = ctx.getResources().getIntArray(
				R.array.int_array_counter_type_value);
		for (int i = 0; i < itemsValue.length; i++) {
			if (itemsValue[i] == itemValue) {
				return itemsName[i];
			}
		}
		return "";
	}

	/*
	 * Handler of Anniversary Date
	 */
	public void setPrefAnniversary(String dt) {
		fnSavePref(prefBasicAnniKey, dt);
	}

	public void setPrefAnniversary(Date dt) {
		setPrefAnniversary(getDateFormatter().format(dt));
	}

	public Date getPrefAnniversary() {
		String toDay = getDateFormatter().format(new Date());
		String dtString = (String)fnGetPref(prefBasicAnniKey, toDay);
		Date prefAnniversaryDate;
		try {
			prefAnniversaryDate = getDateFormatter().parse(dtString);
		} catch (ParseException e) {
			prefAnniversaryDate = new Date();
		}
		return prefAnniversaryDate;
	}

	/*
	 * Handler of Layout - Normal RemoteView - Image RemoteView
	 */
	public void setPrefLayout(int layout) {
		fnSavePref(prefBasicLayoutKey, layout);
	}

	public int getPrefLayout() {
		return (Integer)fnGetPref(prefBasicLayoutKey, 0);
	}

	/*
	 * Handler of Widget Alpha
	 */
	public void setPrefAlpha(int alpha) {
		fnSavePref(prefBasicAlphaKey, alpha);
	}

	public int getPrefAlpha() {
		return (Integer)fnGetPref(prefBasicAlphaKey, 85);
	}

	public int getPrefBasicAlphaIn255Scale() {
		int alpha = getPrefAlpha();
		int alphaIn255 = (int) (((double) 255 / 100) * alpha);
		return alphaIn255;
	}

	/*
	 * Handler of Counter Widget BG
	 */
	public int getPrefCounterBG() {
		int defaultColor = ctx.getResources().getColor(R.color.color_core_grey);
		return (Integer)fnGetPref(prefCountBgKey, defaultColor);
	}

	public void setPrefCounterBG(int color) {
		fnSavePref(prefCountBgKey, color); 
	}

	/*
	 * Handler of Counter Widget FG
	 */
	public int getPrefCounterFontBG() {
		int defaultColor = ctx.getResources().getColor(R.color.color_white);
		return (Integer)fnGetPref(prefCountFontBgKey, defaultColor);
	}

	public void setPrefCounterFontBG(int color) {
		fnSavePref(prefCountFontBgKey, color);
	}

	/*
	 * Handler of Counter Display Pattern - ??Y - ??M - ??D - ??Y ??M ??D
	 */
	public int getPrefCounterPattern() {
		return (Integer)fnGetPref(prefCountPatternKey, 1);
	}

	public String getPrefCounterPatternName() {
		int itemValue = getPrefCounterPattern();
		final String[] itemsName = ctx.getResources().getStringArray(
				R.array.string_array_tagetdt_pattern_name);
		final int[] itemsValue = ctx.getResources().getIntArray(
				R.array.int_array_tagetdt_pattern_value);
		for (int i = 0; i < itemsValue.length; i++) {
			if (itemsValue[i] == itemValue) {
				return itemsName[i];
			}
		}
		return "";
	}

	public void setPrefCounterPattern(String prefTargetDatePattern) {
		final String[] itemsName = ctx.getResources().getStringArray(
				R.array.string_array_tagetdt_pattern_name);
		final int[] itemsValue = ctx.getResources().getIntArray(
				R.array.int_array_tagetdt_pattern_value);
		for (int i = 0; i < itemsName.length; i++) {
			if (itemsName[i].equalsIgnoreCase(prefTargetDatePattern)) {
				setPrefCounterPattern(itemsValue[i]);
			}
		}
	}

	private void setPrefCounterPattern(int prefTargetDatePattern) {
		fnSavePref(prefCountPatternKey, (Integer) prefTargetDatePattern);
	}

	/*
	 * Handler of Counter Theme - Different type of Fonts to Display
	 */
	public int getPrefTheme() {
		return (Integer)fnGetPref(prefCountThemeKey, 1);
	}

	public void setPrefTheme(int prefTheme) {
		fnSavePref(prefCountThemeKey, prefTheme);
	}

	/*
	 * Handler of Top Text BG
	 */
	public int getPrefTtextBG() {
		int defaultColor = ctx.getResources().getColor(R.color.color_deep_grey);
		return (Integer)fnGetPref(prefTTextBgKey, defaultColor);
	}

	public void setPrefTtextBG(int color) {
		fnSavePref(prefTTextBgKey, color);
	}

	/*
	 * Handler of Top Text FG
	 */
	public int getPrefTtextFontBG() {
		int defaultColor = ctx.getResources().getColor(R.color.color_white);
		return (Integer)fnGetPref(prefTTextFontBgKey, defaultColor);
	}

	public void setPrefTtextFontBG(int color) {
		fnSavePref(prefTTextFontBgKey, color);
	}

	/*
	 * Handler of Top Text Font Size
	 */
	public int getPrefTtextSize() {
		return (Integer)fnGetPref(prefTTextSizeKey, 9);
	}

	public void setPrefTtextSize(int size) {
		fnSavePref(prefTTextSizeKey, size);
	}

	/*
	 * Handler of Top Text String
	 */
	public String getPrefTText() {
		return (String)fnGetPref(prefTTextKey, "");
	}

	public void setPrefTText(String prefTText) {
		fnSavePref(prefTTextKey, prefTText);
	}

	/*
	 * Handler of Bottom Text BG
	 */
	public int getPrefBtextFontBG() {
		int defaultColor = ctx.getResources().getColor(R.color.color_white);
		return (Integer)fnGetPref(prefBTextFontBgKey, defaultColor);
	}

	public void setPrefBtextFontBG(int color) {
		fnSavePref(prefBTextFontBgKey, color);
	}

	/*
	 * Handler of Bottom Text FG
	 */
	public int getPrefBtextBG() {
		int defaultColor = ctx.getResources().getColor(R.color.color_deep_grey);
		return (Integer)fnGetPref(prefBTextBgKey, defaultColor);
	}

	public void setPrefBtextBG(int color) {
		fnSavePref(prefBTextBgKey, color);
	}

	/*
	 * Handler of Bottom Text Size
	 */
	public int getPrefBtextSize() {
		return (Integer)fnGetPref(prefBTextSizeKey, 9);
	}

	public void setPrefBtextSize(int size) {
		fnSavePref(prefBTextSizeKey, size);
	}

	/*
	 * Handler of Bottom Text String
	 */
	public String getPrefBText() {
		return (String)fnGetPref(prefBTextKey, "");
	}

	public void setPrefBText(String prefBText) {
		fnSavePref(prefBTextKey, prefBText);
	}
	
	/*
	 * Remove All Pref for widget
	 * */
	public void removePrefForWidget() {
		if (widgetConfigMode==true) {
			SharedPreferences.Editor edit = sPref.edit();
	        edit.remove(prefBasicCountTypeKey+appId);
	        edit.remove(prefBasicAnniKey+appId);
	        edit.remove(prefBasicLayoutKey+appId);
	        edit.remove(prefBasicAlphaKey+appId);
	        edit.remove(prefCountBgKey+appId);
	        edit.remove(prefCountFontBgKey+appId);
	        edit.remove(prefCountPatternKey+appId);
	        edit.remove(prefCountThemeKey+appId);
	        edit.remove(prefTTextKey+appId);
	        edit.remove(prefTTextBgKey+appId);
	        edit.remove(prefTTextSizeKey+appId);
	        edit.remove(prefTTextFontBgKey+appId);
	        edit.remove(prefBTextKey+appId);
	        edit.remove(prefBTextBgKey+appId);
	        edit.remove(prefBTextSizeKey+appId);
	        edit.remove(prefBTextFontBgKey+appId);	        
	        edit.commit();
		}
	}
}
